package com.zlt.service.impl;

import com.zlt.dao.AdminDao;
import com.zlt.dto.AdminDTO;
import com.zlt.entity.Admin;
import com.zlt.entity.Company;
import com.zlt.entity.License;
import com.zlt.entity.Question;
import com.zlt.service.AdminService;
import com.zlt.vo.Pager;

import java.sql.SQLException;
import java.util.List;

public enum AdminServiceImpl implements AdminService {
    INSTANCE;

    AdminServiceImpl() {
    }

    private final AdminDao dao = AdminDao.getInstance();


    @Override
    public Admin isExistedUsername(AdminDTO adminDTO) {
        try {
            return dao.existedByUsername(adminDTO.getUsername());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Pager<Company> companyInfo(Integer page, Integer limit) {
        try {
            Pager<Company> pager = new Pager<>();
            List<Company> companies = dao.listCompany((page - 1) * limit, limit);
            long l = dao.countCompany(0) + dao.countCompany(1) + dao.countCompany(2);
            if (companies.isEmpty())
                return null;
            else {
                pager.setTotalCount(l);
                pager.setData(companies);
                return pager;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Pager<Company> companyInfo(Integer page, Integer limit, Integer state) {
        try {
            Pager<Company> pager = new Pager<>();
            List<Company> companies = dao.listCompany((page - 1) * limit, limit, state);
            long l = dao.countCompany(state);
            if (companies.isEmpty())
                return null;
            else {
                pager.setTotalCount(l);
                pager.setData(companies);
                return pager;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public Pager<Question> listQuestions(Integer page, Integer limit, Boolean isAll, Integer... state) {
        if (!isAll && state.length >= 2)
            return null;
        try {
            Pager<Question> pager = new Pager<>();
            if (isAll) {
                List<Question> questions = dao.listQuestions((page - 1) * limit, limit);
                if (questions.isEmpty())
                    return null;
                else {
                    long l = 0L;
                    for (Integer integer : state) {
                        l += dao.countQuestion(integer);
                    }
                    pager.setTotalCount(l);
                    pager.setData(questions);
                    return pager;
                }
            } else {
                List<Question> questions = dao.listQuestions((page - 1) * limit, limit, state[0]);
                if (questions.isEmpty())
                    return null;
                else {
                    long l = dao.countQuestion(state[0]);
                    pager.setTotalCount(l);
                    pager.setData(questions);
                    return pager;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer changeCompanyState(Integer cid, Integer state) {
        try {
            return dao.updateCompanyState(cid, state);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<License> findLicenses(Integer cid) {
        try {
            return dao.selectLicencesByCid(cid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
